package com.schoolcloud.system.controller;

import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.common.model.R;
import com.schoolcloud.system.entity.SysRole;
import com.schoolcloud.system.query.SysRoleQuery;
import com.schoolcloud.system.service.SysRolePermissionService;
import com.schoolcloud.system.service.SysRoleService;
import com.schoolcloud.system.dto.SysRoleCreateDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("system/role")
public class SysRoleController {

    private final SysRoleService sysRoleService;
    private final SysRolePermissionService sysRolePermissionService;

    @RequestMapping("code/user")
    public R<List<String>> getRoleCodeListByUser(@RequestParam String userId) {
        List<String> roles = sysRoleService.getRoleCodeListByUser(userId);
        return R.ok(roles);
    }

    @GetMapping("/{roleId}")
    public R<SysRole> getRoleById(@PathVariable String roleId) {
        SysRole role = sysRoleService.getRoleById(roleId);
        return R.ok(role);
    }

    @GetMapping("/info/{roleId}")
    public R<SysRoleCreateDTO> getRoleInfoById(@PathVariable String roleId) {
        SysRoleCreateDTO role = sysRolePermissionService.getRoleInfoByRoleId(roleId);
        return R.ok(role);
    }

    @PostMapping
    public R createSysRole(@RequestBody SysRoleCreateDTO createDTO) {
        sysRolePermissionService.createSysRole(createDTO);
        return R.ok();
    }
    @PutMapping
    public R modifySysRole(@RequestBody SysRoleCreateDTO createDTO) {
        sysRolePermissionService.modifySysRole(createDTO);
        return R.ok();
    }

    @GetMapping("/page")
    public R<PageDTO<SysRole>> getRolePage(SysRoleQuery query) {
        PageDTO<SysRole> page = sysRoleService.getRolePage(query);
        return R.ok(page);
    }
}

